#!/bin/bash

usage()
{
	printf "Usage: \n [Release]:\tchangeReleaseVersion.sh A1.1 A1.1 A1.1 A1.0 A1.1\n"
	printf "\t\t\t Note: This script put at ./omci_v1/ if you change src to bin\n"
	printf "\t\t 1st = omci user application version \n"
	printf "\t\t 2nd = omci fc driver version\n"
	printf "\t\t 3th = omci ca8279 driver version\n"
	printf "\t\t 4th = omci rg driver version\n"
	printf "\t\t 5th = omci rtk driver version\n"
	
}


if [ $# -lt 5 ]; then
	usage
	exit 1
fi

BIN_OMCI_APP="omci_app"
BIN_OMCI_CLI="omcicli"

RELEASE_VERSION="\\\\\"$1\\\\\""
		
	sed -i '/^RELEASE_SUBVERSION/d' \
		"./Makefile" && test 0 -ne $? && exit 1
		
	if [ -z "`cat ./Makefile |\
		 grep "^RELEASE_VERSION" | head -n 1`" ]; then
		# Not exist RELEASE_VERSION variable
		sed -i "20 a RELEASE_VERSION\t\t\t= "${RELEASE_VERSION}""\
			 "./Makefile" && test 0 -ne $? && exit 1
	else
		# exist RELEASE_VERSION variable
		sed -i "s/^RELEASE_VERSION.*\"$/RELEASE_VERSION\t\t\t= \
			"${RELEASE_VERSION}"/g" "./Makefile" && test 0 -ne $? && exit 1
	fi

RTKDRV_RELEASE_VERSION="\\\\\"$5\\\\\""

	# for rtk drv release version
	if [ -z "`cat ./DRV/platform/rtl9607_rtk/Makefile |\
		 grep "^RTKDRV_RELEASE_VERSION" | head -n 1`" ]; then
		# Not exist RTKDRV_RELEASE_VERSION variable
		sed -i "3 a RTKDRV_RELEASE_VERSION\t\t\t= "${RTKDRV_RELEASE_VERSION}""\
			 "./DRV/platform/rtl9607_rtk/Makefile" && test 0 -ne $? && exit 1
	else
		# exist RTKDRV_RELEASE_VERSION variable
		sed -i "s/^RTKDRV_RELEASE_VERSION.*\"$/RTKDRV_RELEASE_VERSION\t\t\t= \
			"${RTKDRV_RELEASE_VERSION}"/g" \
			"./DRV/platform/rtl9607_rtk/Makefile" && test 0 -ne $? && exit 1
	fi

RGDRV_RELEASE_VERSION="\\\\\"$4\\\\\""

	# for rg  drv release version
	if [ -z "`cat ./DRV/platform/rtl9607_rg/Makefile |\
		 grep "^RGDRV_RELEASE_VERSION" | head -n 1`" ]; then
		# Not exist RTKDRV_RELEASE_VERSION variable
		sed -i "3 a RGDRV_RELEASE_VERSION\t\t\t= "${RGDRV_RELEASE_VERSION}""\
			 "./DRV/platform/rtl9607_rg/Makefile" && test 0 -ne $? && exit 1
	else
		# exist RTKDRV_RELEASE_VERSION variable
		sed -i "s/^RGDRV_RELEASE_VERSION.*\"$/RGDRV_RELEASE_VERSION\t\t\t= \
			"${RGDRV_RELEASE_VERSION}"/g" \
			"./DRV/platform/rtl9607_rg/Makefile" && test 0 -ne $? && exit 1
	fi
 
RGDRV_RELEASE_VERSION="\\\\\"$2\\\\\""

	# for fc  drv release version
	if [ -z "`cat ./DRV/platform/rt_fc/Makefile |\
		 grep "^RGDRV_RELEASE_VERSION" | head -n 1`" ]; then
		# Not exist RTKDRV_RELEASE_VERSION variable
		sed -i "3 a RGDRV_RELEASE_VERSION\t\t\t= "${RGDRV_RELEASE_VERSION}""\
			 "./DRV/platform/rt_fc/Makefile" && test 0 -ne $? && exit 1
	else
		# exist RTKDRV_RELEASE_VERSION variable
		sed -i "s/^RGDRV_RELEASE_VERSION.*\"$/RGDRV_RELEASE_VERSION\t\t\t= \
			"${RGDRV_RELEASE_VERSION}"/g" \
			"./DRV/platform/rt_fc/Makefile" && test 0 -ne $? && exit 1
	fi

RGDRV_RELEASE_VERSION="\\\\\"$3\\\\\""

	# for fc  drv release version
	if [ -z "`cat ./DRV/platform/ca8279/Makefile |\
		 grep "^RGDRV_RELEASE_VERSION" | head -n 1`" ]; then
		# Not exist RTKDRV_RELEASE_VERSION variable
		sed -i "3 a RGDRV_RELEASE_VERSION\t\t\t= "${RGDRV_RELEASE_VERSION}""\
			 "./DRV/platform/ca8279/Makefile" && test 0 -ne $? && exit 1
	else
		# exist RTKDRV_RELEASE_VERSION variable
		sed -i "s/^RGDRV_RELEASE_VERSION.*\"$/RGDRV_RELEASE_VERSION\t\t\t= \
			"${RGDRV_RELEASE_VERSION}"/g" \
			"./DRV/platform/ca8279/Makefile" && test 0 -ne $? && exit 1
	fi
